package uk.co.hs.web.controller;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import uk.co.hs.constants.HsConstants;

/**
 * Map2Controller.
 */
public abstract class HsController
{
  protected final static DateTimeFormatter DATE_FORMATTER =  DateTimeFormat.forPattern(HsConstants.SCREEN_DATE_FORMAT);
  protected final static DateTimeFormatter DATE_TIME_FORMATTER =  DateTimeFormat.forPattern(HsConstants.SCREEN_DATE_TIME_FORMAT);

  protected String convertDate(DateTime aDate)
  {
    return aDate == null ? null : DATE_FORMATTER.print(aDate.getMillis());
  }

  protected String convertDateTime(DateTime aDate)
  {
    return aDate == null ? null : DATE_TIME_FORMATTER.print(aDate.getMillis());
  }

  @ExceptionHandler(Exception.class)
  protected ModelAndView handleException(Exception aException)
  {
    return new ModelAndView("hs_Error").addObject("exception", aException);
  }
}
